import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/service/connect_service.dart';
import 'package:ball_lion/widgets/loading/loading_indicator.dart';
import 'package:ball_lion/widgets/loading/no_data_widget.dart';

class LoadingWidget<T> extends StatelessWidget {
  const LoadingWidget(
      {super.key,
      required this.isLoading,
      required this.child,
      required this.data,
      this.noData,
      this.loading = const LoadingIndicator(),
      this.physics,
      this.needScroll = false,
      this.reconnect})
      : _isSliver = false;

  const LoadingWidget.sliver(
      {super.key,
      required this.isLoading,
      required this.child,
      required this.data,
      this.noData,
      this.loading = const LoadingIndicator(isSilver: true),
      this.physics,
      this.needScroll = false,
      this.reconnect})
      : _isSliver = true;

  final dynamic data;
  final Widget? noData;
  final Widget loading;
  final bool isLoading;
  final ScrollPhysics? physics;
  final bool needScroll;
  final Function()? reconnect;
  final Function() child;
  final bool _isSliver;

  @override
  Widget build(BuildContext context) {
    if (Get.find<ConnectService>().hasNetwork == false) {
      return NoDataWidget(
        type: ErrorType.network,
        needScroll: needScroll,
        physics: physics,
        isSliver: _isSliver,
        buttonText: "重新连接",
        onTap: reconnect,
      );
    } else if (isLoading) {
      return loading;
    } else {
      if (data != "escape") {
        if (T == String) {
          if (data == null || data == '') {
            return noData ??
                NoDataWidget(
                    needScroll: needScroll,
                    physics: physics,
                    isSliver: _isSliver);
          } else {
            return child.call();
          }
        } else if (T == int) {
          if (data == null || data == 0) {
            return noData ??
                NoDataWidget(
                    needScroll: needScroll,
                    physics: physics,
                    isSliver: _isSliver);
          } else {
            return child.call();
          }
        } else if (T == bool) {
          if (data == true) {
            return noData ??
                NoDataWidget(
                    needScroll: needScroll,
                    physics: physics,
                    isSliver: _isSliver);
          } else {
            return child.call();
          }
        }
      }
      return child.call();
    }
  }
}
